# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time        : 2021/9/21 17:07
@Author      : Albert Darren
@Contact     : 2563491540@qq.com
@File        : version_space.py
@Version     : Version 1.0.0
@Description : TODO
@Created By  : PyCharm
"""

dataset = {"色泽": ["青绿", "乌黑", "浅白"],
           "根蒂": ["蜷缩", "稍缩"],
           "敲声": ["浊响", "沉闷"]}


# 需要为通配符*单独设计bit_code
def wildcard_bit_code(attr_nums):
    """
    实现对属性任意属性数量进行二进制编码
    :param attr_nums: 某属性的属性值数量
    :return: 通配符*二进制编码，比特位长
    """
    from math import log2, ceil
    index = ceil(log2(attr_nums + 1))
    attr_nums += (2 ** index - 1 == attr_nums)  # 取结果的布尔值相加
    return 2 ** ceil(log2(attr_nums + 1)) - 1, ceil(log2(attr_nums + 1))


def hypothesis_space_codes(data_dict):
    """
    实现对假设空间的每一个假设进行二进制编码
    :param data_dict: 字典形式的数据集
    :return: 假设空间编码列表
    """
    import itertools
    bit_codes = []
    code_range = [list(range(1, len(data_dict[attribute]) + 1)) +
                  [wildcard_bit_code(len(data_dict[attribute]))]
                  for attribute in data_dict]
    for code in itertools.product(*code_range):
        bits_str = ""
        bit_code = map(bin, code)
        for bit_str in bit_code:
            bits_str += bit_str[2:].zfill(2)  # zfill高位补齐0等于通配符二进制比特长度
        bit_codes.append(int(bits_str, base=2))
    return bit_codes


def version_space(pcls, ncls, hy_space):
    pass


# print(hypothesis_space_codes(dataset))
for i in range(1, 8):
    print(wildcard_bit_code(i))
